package com.cycleit.applicationexample.view {
	
	import com.cycleit.applicationexample.view.components.about.About;
	import com.cycleit.applicationexample.view.components.help.Help;
	import com.cycleit.applicationexample.view.components.login.Login;
	import com.cycleit.applicationexample.view.components.overapp.Footer;
	import com.cycleit.applicationexample.view.components.overapp.Header;
	import com.cycleit.applicationexample.view.components.user.UserManager;
	import com.cycleit.cycleframework.entity.NavigationView;
	import com.cycleit.cycleframework.event.ViewStateChangeEvent;
	import com.cycleit.cycleframework.interfaces.INavigableView;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	public class ViewManager extends Canvas implements INavigableView{
		// Container which hosts variable views
		private var _mainContainer:Container;
		// It manages our view navigation
		private var _navigationView:NavigationView = new NavigationView(this as INavigableView,true);
		
		// FIXED VIEWS 
		private var header:Header;
		private var footer:Footer;
		// (END) FIXED VIEWS
		
		
		
		// VARIABLE VIEWS
		private var _loging:Login;
		private var _userManager:UserManager;
		private var _about:About;
		private var _help:Help;
		// (END) VARIABLE VIEWS

		/**
		 * Read only variables.
		 */ 
		public function get navigationView():NavigationView {
			return _navigationView;
		}
		
		public function get mainContainer():Container {
			return _mainContainer;
		}
		
		// NavigateTo method will allow us to change views between state change.
		public function navigateTo(nextState:String):void {
		 	var aux:UIComponent = null;
			switch (nextState){
				case "login": 
					if(!_loging){
						_loging = new Login();
					}
					PopUpManager.addPopUp(_loging,this,true);
					break;
				case "userManager": 
					if(!_userManager){
						_userManager = new UserManager();
					}
					aux = _userManager; 
					break;
				case "about":
					if (!_about) {
						_about = new About();
					}
					aux = _about;
					break;
				case "help":
					if (!_help) {
						_help = new Help();
					}
					aux = _help;
					break;
				case "init": 
					mainContainer.removeAllChildren();
					break;
				default:
					new ViewStateChangeEvent(ViewStateChangeEvent.MODIFY_VIEW,new ArrayCollection(['init']),1).dispatch();
				 	break;
			}
			
			if (aux != null) {
				mainContainer.removeAllChildren();
				mainContainer.addChild(aux);
			} 
		}
		
		//MANEJADORES

		
		// ViewManager
		public function ViewManager() {
			super();
			this.y = 0;
			this.setStyle("right",0);
			this.setStyle("left",0);
			this.setStyle("top",0);
			this.setStyle("bottom",0);
			this.horizontalScrollPolicy = 'off';
			this.verticalScrollPolicy = 'off';
		}
		
		// We override createChildren method to add our fixed views, including mainContainer (container for variable views).
		override protected function createChildren():void {
			super.createChildren();
			if (!header) {
				header = new Header();
				header.x = 0;
				header.y = 0;
				
				addChild(header);
			}
			
			if (!footer) {
				footer = new Footer();
				footer.x = 0;
				footer.setStyle("bottom",0);
				
				addChild(footer);
			}
			
			if (!_mainContainer) {
				_mainContainer = new Canvas();
				_mainContainer.setStyle("right",0);
				_mainContainer.setStyle("left",0);
				_mainContainer.setStyle("top",100);
				_mainContainer.setStyle("bottom",60);
				_mainContainer.verticalScrollPolicy = "off";
				_mainContainer.horizontalScrollPolicy = "off";
				
				addChild(_mainContainer);
			}
			
		}
	}
}